<?php

namespace mpend\modules\shop\models;

use Yii;
use common\models\shop\ShopCart;

/**
 * 购物车结算准备处理模型
 * 
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class CartStatusBatchForm extends \common\base\Model {

    /**
     * @var integer 下单用户ID
     */
    public $user_id;

    /**
     * @var array 结算内容单元
     */
    public $items;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['items'], 'safe'],
            [['user_id'], 'integer'],
        ];
    }

    /**
     * 选中状态准备处理
     * @return boolean
     */
    public function save() {
        if (!$this->validate()) {
            return $this->dumpError('商品准备失败');
        }
        //批量更新
        ShopCart::updateAll([
            'status' => ShopCart::STATUS_DEFAULT
        ], [
            'and',
            ['user_id' => $this->user_id],
            ['!=', 'status', ShopCart::STATUS_PREPARE_ORDER],
        ]);

        $ids = $this->items;
        if (!empty($ids)) {
            ShopCart::updateAll(['status' => ShopCart::STATUS_CHECKED], [
                'and',
                ['user_id' => $this->user_id],
                ['id' => $ids],
                ['!=', 'status', ShopCart::STATUS_PREPARE_ORDER],
            ]);
        }
        return true;
    }

}
